/**
 * 广寒宫
 * 网址:www.guanghangong.xyz
 */
package com.moon.admin.sys.service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.moon.admin.sys.cache.DeptCache;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.moon.admin.framework.domain.MoonTree;
import com.moon.admin.framework.domain.MoonTree.MoonTreeBuilder;
import com.moon.admin.framework.springmvc.config.SpringContextConfig;
import com.moon.admin.framework.util.ToolUtils;
import com.moon.admin.sys.entity.SysDeptEntity;
import com.moon.admin.sys.entity.SysPostEntity;
import com.moon.admin.sys.mapper.SysDeptMapper;

/**
 * 部门管理 服务实现
 * @author moon
 */
@Service
public class SysDeptService extends ServiceImpl<SysDeptMapper, SysDeptEntity>{
	
	/**
     * 查询部门管理树
     * @return 所有部门信息
     */
    public List<SysDeptEntity> getDeptList(){
    	List<SysDeptEntity> list = this.list();
		Map<Integer, List<SysDeptEntity>> groupBy = list.stream().collect(Collectors.groupingBy(SysDeptEntity::getParentId));
		List<SysDeptEntity> deptList =  getChildrenDept(groupBy,0);
		return deptList;
    }
    
    /**
	 * 递归
	 */
	private List<SysDeptEntity> getChildrenDept(Map<Integer, List<SysDeptEntity>> groupBy, Integer parentId){
		List<SysDeptEntity> result = Lists.newArrayList();
		List<SysDeptEntity> topMenuList = groupBy.get(parentId);
		if(topMenuList==null)return result;
		for(SysDeptEntity menu:topMenuList){
			menu.setLabel(menu.getDeptName());
			List<SysDeptEntity> tmpMenuList = groupBy.get(menu.getId());
			if(tmpMenuList!=null && !tmpMenuList.isEmpty()){
				menu.setChildren(getChildrenDept(groupBy,menu.getId()));
			}
			result.add(menu);
		}
		return result;
	}
	
	/**
	 * 获取部门，以树结构展示
	 */
	public List<MoonTree> getDeptTreeList(boolean hasPost){
		List<SysDeptEntity> deptList = this.list();
		Map<Integer, List<SysDeptEntity>> deptGroup = deptList.stream().collect(Collectors.groupingBy(SysDeptEntity::getParentId));//key=parentId
		List<MoonTree> deptTreeList =  getChildrenMenuTree(deptGroup,0);
		if(hasPost) {
			List<SysPostEntity> postList = SpringContextConfig.getBean(SysPostService.class).list();
			Map<Integer, List<SysPostEntity>> postGroup = postList.stream().collect(Collectors.groupingBy(SysPostEntity::getDeptId));//key=deptId
			builderDeptTree(deptTreeList,postGroup);
		}
		return deptTreeList;
	}
	
	/**
	 * 将岗位做部门的子菜单显示
	 */
	private void builderDeptTree(List<MoonTree> deptTreeList,Map<Integer, List<SysPostEntity>> postGroup){
    	if(ToolUtils.isEmpty(deptTreeList))return;
    	for(MoonTree tree:deptTreeList){
    		List<MoonTree> pTree = tree.getChildren()==null?Lists.newArrayList():tree.getChildren();
    		builderDeptTree(pTree,postGroup);
    		List<SysPostEntity> postList = postGroup.get(Integer.valueOf(tree.getValue()));
			if(ToolUtils.isNotEmpty(postList)){
				for(SysPostEntity kv :postList){
					String deptPostName = DeptCache.getDeptById(kv.getDeptId()).getDeptName()+"/"+kv.getPostName();
					MoonTree post = MoonTree.builder().id(kv.getId()+"_"+kv.getDeptId()).value(String.valueOf(kv.getId())).label(deptPostName).type("post").build();
					pTree.add(post);
				}
				tree.setChildren(pTree);
			}
		}
    }
	
	/**
	 * 递归
	 */
	private List<MoonTree> getChildrenMenuTree(Map<Integer, List<SysDeptEntity>> groupBy, Integer parentId){
		List<MoonTree> result = Lists.newArrayList();
		List<SysDeptEntity> topMenuList = groupBy.get(parentId);
		if(topMenuList==null)return result;
		for(SysDeptEntity menu:topMenuList){
			MoonTreeBuilder builder = MoonTree.builder().id(String.valueOf(menu.getId())).value(String.valueOf(menu.getId())).label(menu.getDeptName()).type("dept");
			List<SysDeptEntity> tmpMenuList = groupBy.get(menu.getId());
			if(tmpMenuList!=null && !tmpMenuList.isEmpty()){
				builder.children(getChildrenMenuTree(groupBy,menu.getId()));
			}
			result.add(builder.build());
		}
		return result;
	}
	
	/**
	 * 根据id获取所有子id(不包含自已)
	 */
	public List<SysDeptEntity> getDeptAllChildrenIdById(Integer id){
		return this.baseMapper.getDeptAllChildrenIdById(id);
	}
}